package jm.nj.demo06main.demo05;

import java.util.Random;

/*
*  java.lang.Math类:数学工具类
* 私有了构造方法类中所有的方法都是静态的,可以通过类名直接使用
* */
public class Demo01Math {
    public static void main(String[] args) {
        /*
        * static int abs(int a) 返回int值的绝对值
        * */
        System.out.println(Math.abs(-10));
        System.out.println(Math.abs(10));
        System.out.println(Math.abs(-4.1));
        System.out.println(Math.abs(5.8));
        System.out.println(Math.abs(-0));
        System.out.println("---------");
        /*
        * static double ceil(double a)
        * 返回大于或等于参数a的最小整数(向上取整)
        * */
        System.out.println(Math.ceil(3.99));
        System.out.println(Math.ceil(3.01));
        System.out.println(Math.ceil(-3.99));
        System.out.println(Math.ceil(-3.01));
        System.out.println("--------");

        /*
        * static double floor(double a)
        * 返回小于或者等于a的最大整数(向下取整)
        * */
        System.out.println(Math.floor(3.99));
        System.out.println(Math.floor(3.01));
        System.out.println(Math.floor(-3.99));
        System.out.println(Math.floor(-3.01));
        System.out.println("-----------");
        /*
        * static int max(int a, int b)  返回两个数中的最大值
         * static int min(int a, int b)  返回两个数中的最大小值
        * */
        System.out.println(Math.max(10,20));
        System.out.println(Math.min(1,-1));
        System.out.println("------");

        /*
        * static double pow(double a, double b)
        * a的b次方
        * */
        System.out.println(Math.pow(2,3));
        System.out.println(Math.pow(2,-3));
        System.out.println(Math.pow(-2,-3));
        System.out.println(Math.pow(-2,3));
        System.out.println((int) Math.pow(2,-3));
        System.out.println((int) Math.pow(-2,-3));
        System.out.println("=========");

        /*
        * static double sqrt(double a)
        * 平方根
        * */
        System.out.println(Math.sqrt(4));
        System.out.println(Math.sqrt(2));
        System.out.println(Math.sqrt(3));
        System.out.println(Math.sqrt(-2));//NaN
        //System.out.println(5/0);ArithmeticException: / by zero
        System.out.println(Math.sqrt(5.0/0));//Infinity
        System.out.println(Math.sqrt(5.0%0));//NaN

        /*
        * static long round(double a)
        * 四舍五入
        * */
        long l =Math.round(5.5);
        System.out.println(l);

        l= Math.round(5.499999);
        System.out.println(l);
        System.out.println("========");

        /*
        * static double random()  返回随机数[0,1.0);
        * 来源,也是Random类
        * */
        Random r = new Random();
        for(int i=0;i<10;i++){
            double d = r.nextDouble();//获取随机小数,范围[0.0,1.0)
            System.out.println(d);
        }
        System.out.println("====");
        for (int i=0;i<10;i++){
            double d= Math.random();//获取随机小数,范围[0.0,1.0)
            System.out.println(d);
        }
    }
}
